﻿#include <iostream>
#include <string>
#include <regex>


int main()
{
  
	//<string> 头文件提供了 std::string 类，它是对 C 风格字符串的封装，
	//提供了更安全、更易用的字符串操作功能。
     // 声明并初始化字符串
      std::string greeting = "Hello, World!";
    std::cout << "Greeting: " << greeting << std::endl;

    // 使用 size() 获取字符串长度
    std::cout << "Length of the greeting: " << greeting.size() << std::endl;

    // 使用 empty() 检查字符串是否为空
    std::cout << "Is the greeting empty? " << (greeting.empty() ? "Yes" : "No") << std::endl;

    // 使用 operator[] 访问特定位置的字符
    std::cout << "Character at position 7: " << greeting[7] << std::endl;

    // 使用 substr() 获取子字符串
    std::string sub2 = greeting.substr(7, 5);
    std::cout << "Substring from position 7 with length 5: " << sub2 << std::endl;

    // 使用 find() 查找子字符串
    std::cout << "Position of 'World' in the greeting: " << greeting.find("World") << std::endl;

    // 使用 replace() 替换字符串中的部分内容
    // 替换 'World' 为 'C++'
    std::string modified = greeting;
    std::string::size_type pos = modified.find("World");
    if (pos != std::string::npos) {
        modified.replace(pos, 5, "C++"); // 从位置 pos 开始，替换 5 个字符为 "C++"
    }
    std::cout << "Modified greeting: " << modified << std::endl;




    std::string str = "Hello, World!";

    // size()
    std::cout << "Length: " << str.size() << std::endl;

    // empty()
    std::cout << "Is empty? " << (str.empty() ? "Yes" : "No") << std::endl;

    // operator[]
    std::cout << "First character: " << str[0] << std::endl;

    // at()
    std::cout << "Character at position 7: " << str.at(7) << std::endl;

    // substr()
    std::string sub = str.substr(7, 5);
    std::cout << "Substring from position 7 with length 5: " << sub << std::endl;

    // find()
    size_t pos2 = str.find("World");
    std::cout << "Position of 'World': " << pos2 << std::endl;

    // replace()
    str.replace(pos2, 5, "C++");
    std::cout << "Modified string: " << str << std::endl;

    // append()
    str.append(" How are you?");
    std::cout << "Appended string: " << str << std::endl;

    // insert()
    str.insert(7, " Beautiful");
    std::cout << "String after insert: " << str << std::endl;

    // erase()
    str.erase(7, 10);
    std::cout << "String after erase: " << str << std::endl;

    // clear()
    str.clear();
    std::cout << "String after clear: " << (str.empty() ? "Empty" : "Not empty") << std::endl;

    // c_str()
    str = "Hello, C++!";
    const char* cstr = str.c_str();
    std::cout << "C-style string: " << cstr << std::endl;

    // compare()
    int cmp = str.compare("Hello, C++!");
    std::cout << "Comparison result: " << cmp << std::endl;

    // find_first_of()
    size_t pos_first_vowel = str.find_first_of("aeiou");
    std::cout << "First vowel at position: " << pos_first_vowel << std::endl;

    // find_last_of()
    size_t pos_last_vowel = str.find_last_of("aeiou");
    std::cout << "Last vowel at position: " << pos_last_vowel << std::endl;

    // find_first_not_of()
    size_t pos_first_non_vowel = str.find_first_not_of("aeiou");
    std::cout << "First non-vowel at position: " << pos_first_non_vowel << std::endl;

    // find_last_not_of()
    size_t pos_last_non_vowel = str.find_last_not_of("aeiou");
    std::cout << "Last non-vowel at position: " << pos_last_non_vowel << std::endl;



    //<regex> 头文件提供了正则表达式的功能，允许开发者使用一种非常灵活的方式来搜索、
    //替换或分割字符串。
    std::regex re("\\d+");          // 匹配数字
    std::string s = "12345";

    if (regex_match(s, re)) //整体匹配
        std::cout << "匹配成功\n";
    else
        std::cout << "匹配失败\n";



    std::string text = "Hello, World!";
    std::regex pattern("^[a-zA-Z]+, [a-zA-Z]+!$");
    //检查字符串是否匹配正则表达式
    if (regex_match(text, pattern)) {
        std::cout << "The string matches the pattern." << std::endl;
    }
    else {
        std::cout << "The string does not match the pattern." << std::endl;
    }



    //在字符串中搜索匹配项
    text = "123-456-7890 and 987-654-3210";
    std::regex pattern2("\\d{3}-\\d{3}-\\d{4}");

    std::smatch matches;
    while (regex_search(text, matches, pattern2)) {
        std::cout << "Found: " << matches[0] << std::endl;
        text = matches.suffix().str();  // 继续查找剩余部分
    }


    //替换字符串中的匹配项
    text = "Hello, World!";
    std::regex pattern3("World");
    std::string replacement = "Universe";

    std::string result = regex_replace(text, pattern3, replacement);

    std::cout << "Original: " << text << std::endl;
    std::cout << "Modified: " << result << std::endl;





    std::regex re2("\\d+");
    std::string s2 = "abc123def456";

    // 查找第一个匹配
    std::smatch m;
    if (regex_search(s2, m, re2))
        std::cout << "第一个匹配: " << m.str() << std::endl;

    // 遍历所有匹配
    for (std::sregex_iterator it(s2.begin(), s2.end(), re2), end_it; it != end_it; ++it)
        std::cout << "匹配到: " << it->str() << std::endl;

    // 替换
    std::string result2 = regex_replace(s2, re2, "#");
    std::cout << result2 << std::endl;

}
